﻿using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ProcessCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ProcessHandler
{
    public class DelProcedureCommandHandler : IRequestHandler<DelProcedureCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;

        public DelProcedureCommandHandler(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<APIResult<object>> Handle(DelProcedureCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.code = APIResultCode.success;
            result.msg = "删除成功";
            var ids = request.ProcedureIds.Split(',').Select(int.Parse).ToList();

            // 一次性查询所有要删除的计划记录
            var itemsToDelete = await db.Queryable<Procedure>().Where(p => ids.Contains(p.ProcedureId)).ToListAsync();

            await db.Deleteable(itemsToDelete).ExecuteCommandAsync();


            return result;
        }
    }
}
